#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define COL 9
#define ROW 9
#define COLS COL + 2
#define ROWS ROW + 2
#define EASY_COUNT 10   // 雷的数量（简单模式）
#define CONTINUE 1     // 游戏继续
#define OVER 0			// 失败
#define WIN -1           // 胜利

void menu(void); // 菜单
void init(char str[][ROWS], int, int, char); //  初始化
void print(char str[][ROWS], int, int); // 输出
void game(void); // 游戏flow
int checkWin(char str[][COLS], int row, int col);  // 判断是否查出全部雷
int checkMine(char str1[][COLS], char str2[][COLS], int row, int col); // 排查雷

int main()
{
	srand((size_t)time(NULL));
	int off = 0;
	do
	{
		menu();
		printf("input>");
		scanf("%d", &off);
		switch (off)
		{
		case 1:
			printf("--------------------扫雷游戏-------------------\n");
			printf("---------------------start--------------------\n\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("错误值，请重新输入\n");
			break;
		}
	} while (off);
	return 0;
}


void game(void)
{
	char mine[ROWS][COLS] = { 0 }; // 存放布置好的雷的信息
	char show[ROWS][COLS] = { 0 }; // 存放排查雷的信息
	init(mine, ROWS, COLS, '0');   // '0'表示没有雷
	init(show, ROWS, COLS, '*');   // '*'表示未知，还没有排查这里

	print(show, ROW, COL);
	setMine(mine, ROW, COL);
	print(mine, ROW, COL);     // 打印雷的位置

	int ret = 1;
	// 排查雷
	while (ret)
	{
		
		ret = checkMine(mine, show, ROWS, COLS);
		print(show, ROW, COL);
		if(ret != OVER)    // underline
			ret = checkWin(show, ROW, COL);
		switch (ret)
		{
		case OVER:
			printf("------------------游戏失败------------------\n\n");
			printf("-------------------over--------------------\n");
			return;
		case CONTINUE:
			break;
		case WIN:
			printf("------------------游戏成功------------------\n\n");
			printf("--------------------over-------------------\n");
			return;
			break;
		}
	}
	

}
int checkWin(char str[][COLS], int row, int col)
{
	int count = 0;
	for (int i = 1; i <= row ; i++)
	{
		for (int j = 1; j <= col ; j++)
		{
			if (str[i][j] == '*')
				count++;
			if (count > EASY_COUNT)
				break;
		}
	}
	if (count == EASY_COUNT)
		return WIN;      // 场上只有10个*表示成功找出了雷
}

int checkMine(char str1[][COLS], char str2[][COLS], int row, int col)
{
	int x, y;
	printf("输入下标>");
	
	while (1)
	{
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row - 2 && y >= 1 && y <= col - 2)
		{
			int count = 0;
			if (str1[x][y] == '1')    // 踩雷了
			{
				str2[x][y] = '1';
				return OVER;
			}

			// 输出周围8个格子 雷的个数
			for (int i = x - 1; i <= x + 1; i++)
			{
				for (int j = y - 1; j <= y + 1; j++)
				{
					if (str1[i][j] == '1')
						count++;
				}
			}
			str2[x][y] = (char)(count + '0');
			return CONTINUE;
		}
		else {
			printf("下标错误，请重新输入>");
		}
	}
	
}
void menu(void) {
	printf("*******************************\n");
	printf("**********  1、 play  *********\n");
	printf("**********  0、 exit  *********\n");
	printf("*******************************\n");
}
void init(char str[][ROWS], int row, int col, char ch)
{
	int i, j;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
			str[i][j] = ch;
	}
}
void print(char str[][ROWS], int row, int col)
{
	int i, j;
	for (i = 0; i <= col ; i++)
		printf("%d ", i);      // 输出列标
	printf("\n");

	for (i = 1; i <= row; i++)
	{
		printf("%d ", i );   // 输出行号
		for (j = 1; j <= col; j++)
			printf("%c ", str[i][j]);
		printf("\n");
	}
	printf("\n");
}

setMine(char str[][ROWS], int row, int col)
{
	int count = EASY_COUNT;
	
	while (count)
	{
		int rx = rand() % row + 1;
		int ry = rand() % col + 1;   // 产生雷的随机下标
		if (str[rx][ry] == '0')
		{
			str[rx][ry] = '1';
			count--;
		}
	}
}
